<?php
// +----------------------------------------------------------------------
// | Site:  [ http://www.yzmcms.com]
// +----------------------------------------------------------------------
// | Copyright: 袁志蒙工作室，并保留所有权利
// +----------------------------------------------------------------------
// | Author: YuanZhiMeng <214243830@qq.com>
// +---------------------------------------------------------------------- 
// | Explain: 这不是一个自由软件,您只能在不用于商业目的的前提下对程序代码进行修改和使用，不允许对程序代码以任何形式任何目的的再发布！
// +----------------------------------------------------------------------

defined('IN_YZMPHP') or exit('Access Denied'); 
yzm_base::load_controller('common', 'admin', 0);
yzm_base::load_common('lib/content_form'.EXT, 'admin');
yzm_base::load_common('function/hr.func.php');
yzm_base::load_sys_class('page','',0);

class hr_position extends common {

	/**
	 * 岗位列表
	 */
	public function init() {
		$of = input('get.of');
		$or = input('get.or');
		$of = in_array($of, array('p.id','p.department_id','p.name','p.add_time','p.status')) ? $of : 'id';
		$or = in_array($or, array('ASC','DESC')) ? $or : 'ASC';
		//$department_list = D('hr_department')->where(['display'=>1])->order("id asc,listorder asc")->select();
		$department_id = $_GET['department_id'] ?? 0;
		$get = $_GET;
		$where = '1=1';
		$dosubmit = $get['dosubmit'] ?? ''; 
		if($dosubmit == 1){
			if($get['status']!=''){
				$where .= ' AND p.status = '.$get['status'];
			}
			if(trim($get['name'])!=''){
				$where .=' AND p.name LIKE "%'.$get['name'].'%"';
			}
			if($get['department_id']!=''){
				$list = D('hr_department')->field('id')->where('parentid='.$get['department_id'])->select();
				//print_r($list);exit;
				if(!$list){
					$where .= ' AND p.department_id = '.$get['department_id'];
				}else{
					$ids = array_column($list, 'id');
					$where .= ' AND p.department_id in ('.implode(',',$ids).')';	
				}	
			}
		}
		
		$tree = yzm_base::load_sys_class('tree');
		$department_list = D('hr_department')->where(['display'=>1])->order('listorder ASC,id ASC')->limit(1000)->select();
		$array = array();
		foreach($department_list as $v) {
			$v['selected'] = $v['id'] == $department_id ? 'selected' : '';
			$array[] = $v;
		}
		$str  = "<option value='\$id' \$selected>\$spacer \$name</option>";
		$tree->init($array);
		$select_menus = $tree->get_tree(0, $str);
		
		$total = D('hr_position')->alias('p')->where($where)->total();
		$page = new page($total, 30);
		$data = D('hr_position')->alias('p')->field('p.*')->where($where)->order("$of $or")->limit($page->limit())->select();
		foreach($data as &$v){
			$v['total'] = D('hr_member')->where(['position_id'=>$v['id'],'status'=>1])->total();
			$v['department_name'] = department($v['department_id']);
		}
		include $this->admin_tpl('hr/position_list');
	}
	
	
	/**
	 * 删除岗位
	 */
	public function delete() {
		$id = intval($_GET['id']);
		D('hr_position')->delete(array('id'=>$id));
		//删除缓存
		delete_all('HR_CONFIG:*');
		showmsg(L('operation_success'), '', 1);
	}
	
	/**
	 * 删除内容
	 */
	public function del() {
		if($_POST && is_array($_POST['ids'])){
			D('hr_position')->delete($_POST['ids'],true);
			//删除缓存
			delete_all('HR_CONFIG:*');
		}
		return_json(array('status'=>1,'message'=>L('operation_success')));
	}
	
	/**
	 * 添加岗位
	 */
	public function add() {
		$db = D('hr_position');
		if(is_post()) {
			$_POST['add_time'] = time();
			$post = $_POST;
			$info = D('hr_position')->where(['department_id'=>$post['department_id'],'name'=>$post['name']])->find();
			if($info){
				return_json(array('status'=>0,'message'=>'岗位已经存在，请勿重复添加'));
			}
			$r = $db->insert($post, true);
			if(!$r){
				showmsg(L('operation_failure'), 'stop');
			}else{
				//删除缓存
				delete_all('HR_CONFIG:*');
				return_json(array('status'=>1,'message'=>L('operation_success')));
			}
		}else{
			$department_id = isset($_GET['department_id']) ? intval($_GET['department_id']) : 0;
			$tree = yzm_base::load_sys_class('tree');
			$data = D('hr_department')->where(['display'=>1])->order('listorder ASC,id ASC')->limit(1000)->select();
			$array = array();
			foreach($data as $v) {
				$v['selected'] = $v['id'] == $department_id ? 'selected' : '';
				$array[] = $v;
			}
			$str  = "<option value='\$id' \$selected>\$spacer \$name</option>";
			$tree->init($array);
			$select_menus = $tree->get_tree(0, $str);
			include $this->admin_tpl('hr/position_add');
		}
	}
	
	
	/**
	 * 编辑岗位
	 */
	public function edit() {				
		$db = D('hr_position');
		if(isset($_POST['dosubmit'])) {
			$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
			if($db->update($_POST, array('id' => $id), true)){
				//删除缓存
				delete_all('HR_CONFIG:*');
				return_json(array('status'=>1,'message'=>L('operation_success')));
			}else{
				return_json();
			}
			
		}else{
			$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
			$r = $db->where(array('id' => $id))->find();
			if($r) extract($r);

			$tree = yzm_base::load_sys_class('tree');
			$department_list = D('hr_department')->where(['display'=>1])->order('listorder ASC,id ASC')->limit(1000)->select();
			$array = array();
			foreach($department_list as $v) {
				$v['selected'] = $v['id'] == $r['department_id'] ? 'selected' : '';
				$array[] = $v;
			}
			$str  = "<option value='\$id' \$selected>\$spacer \$name</option>";
			$tree->init($array);
			$select_menus = $tree->get_tree(0, $str);

			include $this->admin_tpl('hr/position_edit');
		}
	}
	
	public function text(){
		$id = $_GET['id'];
		if(!$id){
			exit('参数错误！');
		}
		$info = D('hr_position')->where(array('id' => $id))->find();
		include $this->admin_tpl('hr/position_text');
	}


	/**
	 * 岗位排序
	 */
	public function order() {
		if(isset($_POST['dosubmit'])) {
			$db = D('hr_department');
			foreach($_POST['listorders'] as $id => $listorder) {
				$db->update(array('listorder'=>$listorder),array('id'=>$id));
			}
			showmsg(L('operation_success'), '', 1);
		} else {
			showmsg(L('operation_failure'));
		}
	}	


	/**
	 * 岗位隐藏显示
	 */
	public function public_change_status() {
		if(is_post()){
			$id = isset($_POST['id']) ? intval($_POST['id']) : 0;
			$value = isset($_POST['value']) ? intval($_POST['value']) : 0;
			if(D('hr_position')->update(array('status'=>$value), array('id' => $id))){
				//删除缓存
				delete_all('HR_CONFIG:*');
				return_json(array('status'=>1,'message'=>L('operation_success')));
			}else{
				return_json();
			}
		}
	}
	
}